import Vue from 'vue'
import Router from 'vue-router'
// import store from '../store'
// import {getCookie} from '@utils/cookies'

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: process.env.NODE_ENV === 'production' ? '/system-manager/pc' : '',
  routes: [
    {
      path: '/',
      redirect: '/loginone',
      meta: {
        auth: false
      }
    },
    {
      name: 'login',
      path: '/login',
      component: () => import('../pages/Login'),
      meta: {
        auth: false
      }
    },
    {
      name: 'loginone',
      path: '/loginone',
      component: () => import('../pages/loginone'),
      meta: {
        auth: false
      }
    },
    {
      path: '/main',
      name: '主页',
      component: () => import('../pages/Main'),
      children: [

        {
          path: '/dataSource',
          name: 'dataSource',
          component: () => import('../pages/data/dataSource')
        },
        {
          path: '/dataSourceDetail',
          name: 'dataSourceDetail',
          component: () => import('../pages/data/dataSourceDetail')
        },
        {
          path: '/historyLog',
          name: 'historyLog',
          component: () => import('../pages/data/historyLog')
        },
        {
          path: '/model',
          name: 'model',
          component: () => import('../pages/model')
        },
        {
          path: '/userInfo',
          name: 'userInfo',
          component: () => import('../pages/user/userInfo')
        },
        {
          path: '/',
          name: 'home',
          component: () => import('../pages/home/home')
        },
        {
          path: '/sysSetting',
          name: '全局',
          component: () => import('../pages/sysSetting')
        },
        {
          path: '/index',
          name: '全局',
          component: () => import('../pages/sysSetting/index')
        },
        {
          path: '/orgStruct',
          name: '全局',
          component: () => import('../pages/sysSetting/orgStruct')
        },
        {
          path: '/edit-personal',
          name: '个人信息修改',
          component: () => import('../pages/personal/EditPersonal')
        },
        {
          path: '/identifyinfo',
          name: '个人信息修改',
          component: () => import('../pages/personal/IdentifyInfo')
        },
        {
          path: '/menu',
          name: '菜单组管理',
          component: () => import('../pages/menu')
        },
        {
          path: '/edit-menu/:id?',
          name: '菜单操作',
          component: () => import('../pages/menu/EditMenuGroup')
        },
        {
          path: '/user',
          name: '用户管理',
          component: () => import('../pages/user')
        },
        {
          path: '/edit-user/:id?',
          name: '用户操作',
          component: () => import('../pages/user/EditUser')
        }
      ]
    }
  ]
})


const getAuth = (routes, path) => {
  for (let i = 0; i < routes.length; i++) {
    let route = routes[i]
    if (route.path.indexOf('/:') !== -1) {
      let cursor = route.path.indexOf('/:')
      let routerPath = route.path.substring(0, cursor)
      let currentPath = path.substring(0, cursor)
      if (routerPath === currentPath) return route.meta ? (route.meta.auth || true) : true
    }
    if (route.path === path) {
      return route.meta ? route.meta.auth : true
    }
    if (route.children) {
      return getAuth(route.children, path)
    }
  }
}

const getPath = (routerList, path) => {
  for (let i = 0; i < routerList.length; i++) {
    let x = routerList[i]
    if (x.path.indexOf('/:') !== -1) {
      let cursor = x.path.indexOf('/:')
      let routerPath = x.path.substring(0, cursor)
      let currentPath = path.substring(0, cursor)
      if (routerPath === currentPath) return true
    }
    if (x.path === path) {
      return true
    }
    if (x.children) {
      return getPath(x.children, path)
    }
  }
}

export default router
